package byggeTegning.husGeometri;

//import DEBUGGING.*;  // Search for DEBUG.<INFO|TRACE| ... >

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.media.j3d.Group;
import javax.vecmath.Color3f;

import byggeTegning.geometry.Point3D;


// ***************************************************************************
// *** CLASS: Door
// ***************************************************************************
public class Door extends Block
{ public int slagretning; // NORTH,SOUTH,EAST,WEST
  public int hengsling;   // NORTH,SOUTH,EAST,WEST
    
  public Door
  (String ident,Point3D p0,double width,double height,double depth,int slagretning,int hengsling)
  { super(ident,p0,width,height,depth);
    this.slagretning=slagretning;
    this.hengsling=hengsling;
  }
    
  // ***************************************************************************
  // *** METHOD: getModel3D
  // ***************************************************************************
  public /*abstract*/ Group getModel3D(Color3f c)
  {
//    TransformedGroup3D tg=new TransformedGroup3D();
    Group tg=new Group();
    addDoorModel(tg,c);
    return(tg);
  }  

  private void addDoorModel(Group tg,Color3f c)
  {
    c=new Color3f(rammeFarge);
    tg.addChild(super.getModel3D(c));  // TEST !!!!
  }


  public void drawProjection(int direction,Graphics2D g,Point2D.Double origo2D,double scale,Color c)
  { Rectangle2D.Double r=projection(direction,origo2D,scale);
    switch(direction)
    {
      case NORTH: tegnDoorFasade(g,r,scale,c); break;
      case SOUTH: tegnDoorFasade(g,r,scale,c); break;
      case EAST: tegnDoorFasade(g,r,scale,c); break;
      case WEST: tegnDoorFasade(g,r,scale,c); break;
//      case ABOVE: drawProjectionAbove(g,r,scale,c); break;
//      case BELOW: drawProjectionBelow(g,r,scale,c); break;
//      case VCUT: drawVerticalCut(g,r,scale,c); break;
      case HCUT: drawHorisontalCut(g,r,scale,c); break;
      default: super.drawProjection(direction,g,origo2D,scale,c);
    }  
  }
      
  private void drawHorisontalCut(Graphics2D g,Rectangle2D.Double r,double scale,Color c)
  { if(r.width>r.height)
    { double tykkelse=this.depth*scale;
      String str=""+(int)(this.width*10)+"/"+(int)(this.height*10);
      tegnDoorEastWest(g,str,r.x,r.y,r.width,tykkelse);
    }  
    else
    { double tykkelse=this.width*scale;
      String str=""+(int)(this.depth*10)+"/"+(int)(this.height*10);
      tegnDoorNorthSouth(g,str,r.x,r.y,r.height,tykkelse);
    }  
  }
  
  private void tegnDoorEastWest(Graphics2D g,String str,double x,double y,double width,double tykkelse)
  { double startAngle;
    double arcAngle;
    fillRect(g,x,y-2,width,tykkelse+4,Color.white);
    label(g,str,x+width/2,y+tykkelse/3);
    switch(hengsling)
    {
      case EAST:
      { switch(slagretning)
        { case NORTH:
          { startAngle=90;
            arcAngle=90;  
            drawArc(g,x,y-width,2*width,2*width,startAngle,arcAngle);
            drawRect(g,x+width-tykkelse/2,y-width,tykkelse/2,width);
//            fillRect(g,x+width-tykkelse/2,y-width,tykkelse/2,width,Color.black);
            break;
          }
          case SOUTH:
          { startAngle=180;
            arcAngle=90;  
            drawArc(g,x,y-width+tykkelse,2*width,2*width,startAngle,arcAngle);
            drawRect(g,x+width-tykkelse/2,y+tykkelse,tykkelse/2,width);
//            fillRect(g,x+width-tykkelse/2,y+tykkelse,tykkelse/2,width,Color.black);
            break;
          }
        } break;  
      }
      case WEST:
      { switch(slagretning)
        { case NORTH:
          { startAngle=0;
            arcAngle=90;  
            drawArc(g,x-width,y-width,2*width,2*width,startAngle,arcAngle);
            drawRect(g,x,y-width,tykkelse/2,width);
//            fillRect(g,x,y-width,tykkelse/2,width,Color.black);
            break;
          }
          case SOUTH:
          { startAngle=270;
            arcAngle=90;  
            drawArc(g,x-width,y-width+tykkelse,2*width,2*width,startAngle,arcAngle);
            drawRect(g,x,y+tykkelse,tykkelse/2,width);
//            fillRect(g,x,y+tykkelse,tykkelse/2,width,Color.black);
            break;
          }
        } break;  
      }
    }  
  }  
  
  private void tegnDoorNorthSouth(Graphics2D g,String str,double x,double y,double width,double tykkelse)
  { double startAngle;
    double arcAngle;  
    fillRect(g,x-2,y,tykkelse+4,width,Color.white);
    label(g,str,x+tykkelse/2,y+width/2);
    switch(hengsling)
    {
      case NORTH:
      { switch(slagretning)
        { case EAST:
          { startAngle=270;
            arcAngle=90;  
            drawArc(g,x-width+tykkelse,y-width,2*width,2*width,startAngle,arcAngle);
            drawRect(g,x+tykkelse,y,width,tykkelse/2);
//            fillRect(g,x+tykkelse,y,width,tykkelse/2,Color.black);
            break;
          }
          case WEST:
          { startAngle=180;
            arcAngle=90;  
            drawArc(g,x-width,y-width,2*width,2*width,startAngle,arcAngle);
            drawRect(g,x-width,y,width,tykkelse/2);
//            fillRect(g,x-width,y,width,tykkelse/2,Color.black);
            break;
          }
        } break;
      }
      case SOUTH:  
      { switch(slagretning)
        { case EAST:
          { startAngle=0;
            arcAngle=90;  
            drawArc(g,x-width+tykkelse,y,2*width,2*width,startAngle,arcAngle);
            drawRect(g,x+tykkelse,y+width-tykkelse/2,width,tykkelse/2);
//            fillRect(g,x+tykkelse,y+width-tykkelse/2,width,tykkelse/2,Color.black);
            break;
          }
          case WEST:
          { startAngle=90;
            arcAngle=90;  
            drawArc(g,x-width,y,2*width,2*width,startAngle,arcAngle);
            drawRect(g,x-width,y+width-tykkelse/2,width,tykkelse/2);
//            fillRect(g,x-width,y+width-tykkelse/2,width,tykkelse/2,Color.black);
            break;
          }
        } break;  
      }  
    }
  }  
  

  private void tegnDoorFasade
  (Graphics2D g,Rectangle2D.Double r,double scale,Color c)
  {
    tegnUteDoor(g,r.x,r.y,r.width,r.height,scale);
  }
  
  private void tegnUteDoor(Graphics2D g,double x,double y,double width,double height,double scale)
  {
    fillRect(g,x,y,width,height,rammeFarge);
    drawRect(g,x,y,width,height);
    // Tegn fiskebein
    double x1=x;
    double x2=x+(width/2);
    double x3=x+width;
    double y1=y;
    double y2=y-(width/2);
    double y22=y2;
    double x11=x1;
    double x33=x3;
    double dd=0.15*scale;
    
    int cnt=15;
//    DEBUG.INFO("*** DOOR HEIGHT="+height);
//    if(height<150) cnt=8;
    if((height/width)<1.50) cnt=8;
    
    for(int i=0;i<cnt;i++)
    { y1=y1+dd;
      y2=y2+dd;
      y22=y2; if(y22<y) y22=y;
      x11=x11+dd; if(x11>x2) x11=x2;
      x33=x33-dd; if(x33<x2) x33=x2;
      if(y1>y+height)
      { x1=x1+y1-(y+height);
        x3=x3-y1+(y+height);
        y1=y+height;
      }
      drawLine(g,x1,y1,x11,y22);
      drawLine(g,x3,y1,x33,y22);
    }  
    // Tegn omramming
    double ramme=0.12*scale;//8;
    double utskudd=0.05*scale;
    fillRect(g,x-(ramme+utskudd),y-ramme,width+2*(ramme+utskudd),ramme,rammeFarge);
    drawRect(g,x-(ramme+utskudd),y-ramme,width+2*(ramme+utskudd),ramme);
  
    fillRect(g,x-ramme,y,ramme,height,rammeFarge);
    drawRect(g,x-ramme,y,ramme,height);
  
    fillRect(g,x+width,y,ramme,height,rammeFarge);
    drawRect(g,x+width,y,ramme,height);
    
    fillRect(g,x-(ramme+utskudd),y+height,width+2*(ramme+utskudd),ramme,rammeFarge);
    drawRect(g,x-(ramme+utskudd),y+height,width+2*(ramme+utskudd),ramme);
  }  

}
